/**
 * 
 */
package com.HibernateRecipes.Chapter5.DAO;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.Hib.Util.SessionManager;
import com.HibernateRecipes.Chapter5.Domain.Book;
import com.HibernateRecipes.Chapter5.Domain.Publisher;

/**
 * @author Ignatiues charles arun
 * 
 *         Dec 14, 2010
 */
public class BookDAO
	{

		private static SessionFactory sessionFactory;

		/**
		 * @param args
		 */
		public static void main(String[] args)
			{
				// TODO Auto-generated method stub
				sessionFactory = SessionManager.getSessionfactory();
				Session session = sessionFactory.getCurrentSession();
				session.beginTransaction();

				Publisher[] publisher = new Publisher[4];
				publisher[0] = new Publisher();
				publisher[0].setPublisher("Amazon");
				publisher[0].setPublisher_id(new Long(20));

				publisher[1] = new Publisher();
				publisher[1].setPublisher_id(new Long(30));
				publisher[1].setPublisher("Apress");

				Book[] book = new Book[4];
				book[1] = new Book();
				book[1].setBook_name("Head First Java");
				book[1].setPrice(new Long(400));
				book[1].setPublisher(publisher[0]);
				publisher[0].setBk(book[1]);

				book[2] = new Book();
				book[2].setBook_name("Hibernate Recipes");
				book[2].setPrice(new Long(300));
				book[2].setPublisher(publisher[1]);
				publisher[1].setBk(book[2]);

				book[3] = new Book();
				book[3].setBook_name("Spring");
				book[3].setPrice(new Long(400));

				publisher[2] = new Publisher();
				publisher[2].setPublisher_id(new Long(50));
				publisher[2].setPublisher("Wrox");

				Transaction tx = session.beginTransaction();
				session.saveOrUpdate(book[1]);
				session.saveOrUpdate(book[2]);
				session.saveOrUpdate(book[3]);
				session.saveOrUpdate(publisher[2]);
				// session.saveOrUpdate(publisher[1]);
				// session.save(publisher);
				// session.save(book);
				tx.commit();

			}
	}
